<?php

namespace posts\view;

require_once("posts/model/PostDAL.php");


class PostView {

	private static $TITLE = 'PostView::Title';
	private static $TEXT = 'PostView::Text';
	private static $SUBMIT = 'AddPost';
	private static $POSTID = 'postid';
	private static $FILEUPLOAD = 'fileInput';	
	/**
	 * @var \posts\model\PostDAL $postDAL
	 */
	private $postDAL;

	/**	 
	 * @var string
	 */
	private $message;

	public function __construct(\common\model\BaseDAL $baseDAL) {

		$this->postDAL = new \posts\model\PostDAL($baseDAL);
	}
	
	/**
	 * Get all posts
	 * @return string html
	 */
	public function getHTMLPosts() {
		$posts = $this->postDAL->getAllPosts();
		$html = "<div id='posts'>";
		foreach ($posts as $post) {
			$html .= "<div class='post' id='post-$post->ID'>";
			$html .= "<h2><a href='?postid=$post->ID'>$post->Title</a></h2>";
			$html .= "<h3> By $post->username</h3>";
			$html .= "<img src='$post->imagePath' />";
			$html .= "<p>$post->Text</p>";
			$html .= "</div>";			
		}
		$html .= "</div>";
		return $html;
	}

	/**
	 * Add post 
	 * @return string html
	 */
	public function getAddPosts() {
		$html = 
		"<div id='addPostForm'>
			<form action='?".self::$SUBMIT."' method='post' enctype='multipart/form-data'>				
				<fieldset>		
					<legend>Add post</legend>
					<p>$this->message</p>
					<input id='TitleID' placeholder='Title' name='".self::$TITLE."' /> 
					<input id='uploadId' type='file' name='".self::$FILEUPLOAD."' />
					<textarea maxlength='255' id='PostTextID' 
					 placeholder='Post text' rows='4' cols='17' name='".self::$TEXT."' /></textarea>
					<input id='SubmitID' type='submit' value='Send' name='".self::$SUBMIT."' /> 
				</fieldset>
			</form>
		</div>";

		return $html;
	}

	/**
	 * @return bool
	 */
	public function showSinglePost() {
		if (isset($_GET[self::$POSTID]) && !empty($_GET[self::$POSTID])) {
			return true;
		}
		return false;
	}

	public function postFailed(\posts\model\Post $post, $uploadedFile) {
		if ($post->Title == "") {
			$this->message = "<p>Enter a title</p>";
		}
		else if ($uploadedFile["type"] == "") {
			$this->message = "<p>Filens format godkändes inte</p>";
		}
	}

	/**
	 * Get a single post
	 * @param  int $postID
	 * @return string html
	 */
	public function getSinglePost($postID) {
		$post = $this->postDAL->getSinglePost($postID);
		$html = "<div id='posts'>";
			$html .= "<div class='post' id='post-$post->ID'>";
			$html .= "<h2>$post->Title</h2>";
			$html .= "<h3> By $post->username</h3>";
			$html .= "<img src='$post->imagePath' />";
			$html .= "<p>$post->Text</p>";
			$html .= "</div>";
		$html .= "</div>";

		return $html;
	}

	/**
	 * @return int
	 */
	public function getPostID() {
		if (isset($_GET[self::$POSTID])) {
			return $_GET[self::$POSTID];
		}
	}

	/**
	 * returns a post object 
	 * @param  \common\model\User $user
	 * @return  \posts\model\Post
	 */
	public function getPostData(\common\model\User $user) {
		$username = $user->username;
		$id = $user->id;
		$title = strip_tags($this->getTitle());
		$text = strip_tags($this->getText());
		$image = $this->getUploadedFile();
		return new \posts\model\Post($username, $id, $title, $text, "");
	}

	/**
	 * @return File $_FILES[]
	 */
	public function getUploadedFile() {
		if (isset($_FILES[self::$FILEUPLOAD])) {
			return $_FILES[self::$FILEUPLOAD];
		}
	}
	
	/**
	 * @return string
	 */
	private function getTitle() {
		if (isset($_POST[self::$TITLE])) {
			return $_POST[self::$TITLE];
		}
	}

	/**
	 * @return string
	 */
	private function getText() {
		if (isset($_POST[self::$TEXT])) {
			return $_POST[self::$TEXT];
		}
	}

	/**
	 * @return bool
	 */
	public function userPosting() {

		return isset($_POST[self::$SUBMIT]);
	}

	/**
	 * @return bool
	 */
	public function addingPost() {

		return isset($_GET[self::$SUBMIT]);
	}

	/**
	 * @return string html
	 */
	public function getPostButton() {
		$html = "<div id='createPostBtn'>
					<a class='footerButton' href='?".self::$SUBMIT."'>New post</a>
				 </div>";
		return $html;
	}

	
}
